//==============================================================================
#include "CBall.h"

//==============================================================================
CBall::CBall() {
}

//------------------------------------------------------------------------------
CBall::~CBall() {
}

//==============================================================================
bool CBall::OnInit(int tx, int ty) {
	X = tx * 40;
	Y = ty * 40;

	vx = 0;
	vy = 0;

	color = rand() % (CScore::Score.Level + 4);

    Width = Height = AnimWidth = AnimHeight = 40;

    Animate = Focus = Select = false;
    Live = true;

    return true;
}

//------------------------------------------------------------------------------
void CBall::OnLoop() {
}

//------------------------------------------------------------------------------
void CBall::OnRender(SDL_Surface* Surf_Display, SDL_Surface* image, int color) {
    if(image == NULL || Surf_Display == NULL) return;

    if (Live) {
        if (Focus && !Select) {
            CSurface::OnDraw(Surf_Display, image, X, Y + 40, color * Width, 40, Width, Height);
            return;
        }
        if (Select) {
            CSurface::OnDraw(Surf_Display, image, X, Y + 40, color * Width, 80, Width, Height);
            return;
        }

        if (Animate) {
            SDL_Surface* buf = SDL_CreateRGBSurface(SDL_HWSURFACE|SDL_SRCCOLORKEY, Width, Height, 32, 0, 0, 0, 0);
            CSurface::OnDraw(buf, image, 0, 0, color * Width, 0, Width, Height);
            CSurface::OnDraw(Surf_Display, CSurface::ScaleSurface(buf, AnimWidth, AnimHeight), X + (Width - AnimWidth) / 2, Y + 40 + (Height - AnimHeight) / 2, 0, 0, Width, Height);
        } else
            CSurface::OnDraw(Surf_Display, image, X, Y + 40, color * Width, 0, Width, Height);
    }
}

//------------------------------------------------------------------------------
void CBall::OnAnimate() {
    if(OldTime + 10 > int(SDL_GetTicks())) {
        return;
    }

    AnimWidth -= 4; AnimHeight -= 4;
    if (AnimWidth == 0 && AnimHeight == 0) {
        Animate = false;
        Live = false;
    }

    OldTime = SDL_GetTicks();
}

//------------------------------------------------------------------------------
void CBall::SetAnimate() {
    Animate = true;
}

//==============================================================================
